﻿using SqlSugar;
using System.Globalization;
using WebApi.Commons.Helpers;
using WebApi.Models.Common;
using WebApi.Models.Views.Account;
using WebApi.Repository;
using WebApi.Services.Interface;

namespace WebApi.Services
{
    /// <summary>
    /// 测试业务
    /// </summary>
    public class DemoService : IDemoService
    {
        #region 依赖注入

        private AccountRepository _accountRepository;

        /// <summary>
        /// 构造方法
        /// </summary>
        public DemoService(AccountRepository accountRepository)
        {
            _accountRepository = accountRepository;
        }

        #endregion

        /// <summary>
        /// 生成实体
        /// 格式可以根据自己的表自定义
        /// </summary>
        public void CreateModel(string tableName)
        {
            var Context = _accountRepository.Context;
            var tables = _accountRepository.Context.DbMaintenance.GetTableInfoList().Where(p => p.Name.StartsWith(tableName));
            foreach (var item in tables)
            {
                //实体名大写
                var items = item.Name.Replace("n_", "").Split("_");
                string entityName = string.Join("", items.Select(p => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(p))) + "Model";
                Context.MappingTables.Add(entityName, item.Name);
                foreach (var col in Context.DbMaintenance.GetColumnInfosByTableName(item.Name))
                {
                    var words = col.DbColumnName.Split('_');
                    var dbcolumnName = string.Join("", words.Select(p => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(p)));
                    Context.MappingColumns.Add(dbcolumnName, col.DbColumnName, entityName);
                }
            }
            Context.DbFirst.IsCreateAttribute().Where(p => p.ToLower().StartsWith(tableName)).CreateClassFile("Models\\Database\\DbName", "WebApi.Models.Database.DbName");
        }

        /// <summary>
        /// 获取账号列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public PageResultModel<UserInfoModel> GetAccountList(GetAccountListRequest request)
        {
            var response = new PageResultModel<UserInfoModel>
            {
                Result = new List<UserInfoModel>()
            };
            var data = _accountRepository.GetAccountList(request, out int totalCount);
            response.TotalCount = totalCount;
            response.Result = data.Select(p =>
            {
                return new UserInfoModel
                {
                    Id = p.Id,
                    Account = p.Account,
                    Name = p.Name,
                };
            }).ToList();
            return response;
        }

        /// <summary>
        /// 导出账号列表
        /// </summary>
        /// <returns></returns>
        public ResultInfoModel ExportAccountList()
        {
            var accounts = _accountRepository.GetList();
            var filePath = $"{ConfigHelper.GetUploadTempPath()}temp/账号列表_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
            // 需要中文列名只需要在实体上加特性：Description
            ExcelHelper.GetExportPath(accounts, filePath);
            return GetResult.Success("导出成功", filePath);
        }
    }
}
